package com.game.springbootstart.service.impl;

import com.game.springbootstart.DTO.LoginUser;
import com.game.springbootstart.entity.Admin;
import com.game.springbootstart.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;  // 假设您使用 MyBatis 来访问数据库

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据库中获取用户信息
        Admin admin = adminMapper.getByUsername(username);

        if (admin == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        if (admin.getStatus()==0){
            throw new UsernameNotFoundException("用户被锁定");
        }

        List<String> authorities = adminMapper.selectRole(admin.getId());
       
        // 如果用户存在，创建并返回 UserDetails 对象
        return new LoginUser(admin,authorities);
    }
}
